//
//  HRRegisterAndForgotPwdViewController.m
//  haierapp
//
//  Created by gaotangzhang on 14-8-24.
//  Copyright (c) 2014年 haier. All rights reserved.
//

#import "HRRegisterAndForgotPwdViewController.h"

@interface HRRegisterAndForgotPwdViewController (){
    NSString *_urlStr;
}

@end

@implementation HRRegisterAndForgotPwdViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //解决布局问题
    if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)]) {
        self.edgesForExtendedLayout=UIRectEdgeNone;
    }
    
    if (_flag==PAGE_FLAG_REGISTER) {
        //注册页面
        self.title=@"注册";
        _urlStr=IDS_REGISTER_URL;
    }else{
        //找回密码
        self.title=@"找回密码";
        _urlStr=IDS_FORGOTPWD_URL;
    }
 
    [self loadWebView];
}

-(void)loadWebView{
    
    //隐藏无网络提示
    [self hideHud];
    
    NSURL *url=[NSURL URLWithString:_urlStr];
    [self.webView loadRequest:[NSURLRequest requestWithURL:url]];
    
    [self.webView loadRequest:[NSURLRequest requestWithURL:url] progress:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
        [self showCustomLoadingHud:kLoading inView:nil];
    } success:^NSString *(NSHTTPURLResponse *response, NSString *HTML) {
        [self hideCustomLoadingHud];
        return HTML;
    } failure:^(NSError *error) {
        [self showHudByType:HUDTYPE_NO_WIFI];
        [self hideCustomLoadingHud];
    }];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/



#pragma mark - UIWebview Delegate Methods
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    
    NSString *urlStr=[NSString stringWithFormat:@"%@",request.URL];
    
    NSString *successFlag;
    
    if (_flag==PAGE_FLAG_FORGETPWD) {
        successFlag=@"find-pwd-success";
    }else{
        successFlag=@"register-success";
    }
    
    NSRange range=[urlStr rangeOfString:successFlag];
    NSUInteger count=urlStr.length;
    
    if (range.location<count) {
        //操作成功
        [self showSuccessHudInWindow:@"操作成功，请登录"];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.navigationController popViewControllerAnimated:YES];
        });
        
        return NO;
    }

    return YES;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    [webView stringByEvaluatingJavaScriptFromString:@"$('header').hide()"];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    NSLog(@"%@",error.description);
}

#pragma mark - HRPorgress Delegate Methods
//重新加载
-(void)tapHUDButtonAction:(id)button{
    [self loadWebView];
}


@end
